package mediadisplay.timeline.contenteditarea 
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author Anders Figenschow
	 */
	public class TextArea extends Sprite
	{
		private var _desc:String;
		private var _width:Number;
		private var _height:Number;
		
		private var descTextFormat:TextFormat;
		private var descTF:TextField;
		private var contTextFormat:TextFormat;
		private var contTF:TextField;
		
		private var contBG:Shape;
		public var protect:Boolean;
		
		private var timer:Timer;
		
		public static var IN_EDIT_MODE:Boolean = false;	// determines if the user is using the textfield.  allows for spacebar to be entered without starting or stopping the track.
		public static const TEXT_CHANGED:String = "textChanged";
		
		public function TextArea(desc:String, protect:Boolean = false, width:Number = 700, height:Number = 22) 
		{
			this.protect = protect;
			_height = height;
			_width = width;
			_desc = desc;
			
			timer = new Timer(10, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
			
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			descTextFormat = new TextFormat;
			descTextFormat.size = 18;
			descTextFormat.color = 0xeeeeee;
			
			descTF = new TextField();
			addChild(descTF);
			descTF.autoSize = TextFieldAutoSize.LEFT;
			descTF.defaultTextFormat = descTextFormat;
			descTF.text = _desc;
			descTF.selectable = false;
			
			contTextFormat = new TextFormat();
			contTextFormat.color = 0;
			contTextFormat.size = 17;
			
			contBG = new Shape();
			contBG.graphics.beginFill(0xffffff);
			contBG.graphics.drawRect(0, 0, _width, _height);
			contBG.graphics.endFill();
			addChild(contBG);
			contBG.x = descTF.width + 5;
			
			contTF = new TextField();
			addChild(contTF);
			contTF.defaultTextFormat = contTextFormat;
			contTF.type = TextFieldType.INPUT;
			contTF.selectable = true;
			contTF.width = _width;
			if (_height > 22) {
				contTF.multiline = true;
				contTF.wordWrap = true;
			} else {
				contTF.multiline = false;
				contTF.wordWrap = false;
			}
			contTF.height = _height;				
			contTF.x = contBG.x;
			contTF.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
			contTF.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			
			deactivate();
		}
		
		private function focusInHandler(e:FocusEvent):void 
		{
			IN_EDIT_MODE = true;
		}
		
		private function focusOutHandler(e:FocusEvent):void 
		{
			IN_EDIT_MODE = false;
		}
		
		private function keyDownHandler(e:KeyboardEvent):void 
		{
			if (e.ctrlKey) return;
			if (e.altKey) return;
			
			timer.reset();
			timer.start();
			
		}
		private function timerComplete(e:TimerEvent):void 
		{
			dispatchEvent(new Event(TEXT_CHANGED));
		}
		
		public function getText():String {
			return contTF.text;
		}
		
		public function setText(txt:String):void {
			contTF.text = txt;
		}
		
		public function deactivate():void {
			contTF.type = TextFieldType.DYNAMIC;
			alpha = 0.5;
			contTF.selectable = false;
		}
		
		public function activate():void {
			if (protect) return;
			
			contTF.type = TextFieldType.INPUT;
			alpha = 1;
			contTF.selectable = true;
			
		}
	}

}